import { defineStore } from "pinia";
import { cartList, cartSelectone, cartUpdatenum, cartRemove, cartSelectall, cartRemoveall } from '../api/cart'
type State = {
    userid: string | null,
    cartLisr: CartList[],
    selectAll: boolean,
    money: number,
    listLength: number,
    listNum: number
}

export const useCart = defineStore(
    'cart-stores',
    {
        state: (): State => {
            return {
                userid: localStorage.getItem('userid'),
                cartLisr: [{} as CartList],
                selectAll: false,
                money: 0,
                listLength: 0,
                // 目前用户的商品购物车数量
                listNum: 0
            }
        },
        getters: {
            // 总价
            totalMoney(state) {
                if(!state.cartLisr) return 0
                return state.cartLisr.reduce((prev, item) => {
                    // item 是当前循环的对象 prev是上一次计算的结果
                    if (item.flag) {
                        return prev + item.originprice * item.discount / 10 * item.num
                    } else {
                        return prev
                    }
                }, 0)
            },
            // 计算商品总数
            total(state) {
                if(!state.cartLisr) return 0
                return state.cartLisr.reduce((prev, item) => {
                    return prev + item.num
                }, 0)
            },
            isxq(state){
                console.log(123);
                
                if(!state.cartLisr.length){
                    this.selectAll = false
                }
            }
        },
        actions: {
            // 获取购物车数据
            async feAcartList() {
                let result = await cartList(this.userid!)
                this.cartLisr = result.data
                console.log(result);
                this.iSselectAll()
                // 数据为空就不需要往下走了
                if (!this.cartLisr) {
                    return
                }
                this.listLength = this.cartLisr.length
            },
            // 更新购物车选定状态
            async feachCartSelectone(cartid: string, flag: boolean) {
                let result = await cartSelectone(cartid, flag)
                // 发生改变之后本地数据也改一下
                let index = this.cartLisr.findIndex(item => item.cartid == cartid)
                this.cartLisr[index].flag = flag
                this.iSselectAll()
            },
            // 更改购物车数量
            async feachCartUpdatenum(cartid: string, num: number) {
                let result = await cartUpdatenum(cartid, num)
                let index = this.cartLisr.findIndex(item => item.cartid == cartid)
                this.cartLisr[index].num = num

            },
            // 删除当个数据
            async feachCartRemove(cartid: string) {
                let result = await cartRemove(cartid)
                // console.log(result);
                let index = this.cartLisr.findIndex(item => item.cartid == cartid)
                this.cartLisr.splice(index, 1)
                this.listLength = this.cartLisr.length

            },
            // 清空购物车
            async feachcartRemoveall() {
                let result = await cartRemoveall(this.userid!)
                this.cartLisr = []
                this.listLength = 0
                this.selectAll = false
            },
            // 全选
            iSselectAll() {
                // 数据为空就不需要往下走了
                if (!this.cartLisr) {
                    return
                }
                // 如果能找到就说明有一个没有被选上
                let index = this.cartLisr.find(item => item.flag == false)
                // 如果能进来就说明没有全部选上
                if (index) {
                    this.selectAll = false

                } else {
                    this.selectAll = true
                }

            },
            // 全选按钮被点击的时候改变所有的状态
            isQx() {
                // 数据为空就不需要往下走了
                if (!this.cartLisr) {
                    return
                }
                if (this.selectAll) {
                    // 为真的时候勾选全部的
                    this.cartLisr.forEach(item => {
                        item.flag = true
                    })
                } else {
                    this.cartLisr.forEach(item => {
                        item.flag = false
                    })
                }
            },
            // 改变全选按钮
            qsBtn() {
                this.selectAll = !this.selectAll
                if(!this.cartLisr) return
                this.isQx()
                cartSelectall(this.userid!, this.selectAll)
            },
           
        }
    }
)